package com.jinzhi.jzweb.controller;


import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.jzapi.common.pojo.SchoolCerUtilsVO;
import com.jinzhi.jzweb.domain.SchoolCertificatesDO;
import com.jinzhi.jzweb.domain.SchoolCertificatesVO;
import com.jinzhi.jzweb.domain.vo.ZykUserBindingVO;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.jzweb.utils.ValuesUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ZykUserBindingDO;
import com.jinzhi.jzweb.service.ZykUserBindingService;
import com.jinzhi.common.utils.Result;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 用户绑定信息导入
 * </pre>
 * <small> 2024-01-09 10:47:34 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/zykUserBinding")
public class ZykUserBindingController extends BaseController {
	@Autowired
	private ZykUserBindingService zykUserBindingService;
	
	@GetMapping()
	String ZykUserBinding(){
	    return "jzweb/zykUserBinding/zykUserBinding";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<ZykUserBindingDO>> list(ZykUserBindingDO zykUserBindingDTO){
		EntityWrapper<ZykUserBindingDO> userBindingDOEntityWrapper =new EntityWrapper<>();
		userBindingDOEntityWrapper.eq("key_id",zykUserBindingDTO.getKeyId());
		if (zykUserBindingDTO.getUserName()!=null&&!zykUserBindingDTO.getUserName().equals("")){
			userBindingDOEntityWrapper.eq("user_name",zykUserBindingDTO.getUserName());
		}
		// 查询列表数据
		Page<ZykUserBindingDO> page = zykUserBindingService.selectPage(getPage(ZykUserBindingDO.class),userBindingDOEntityWrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				ZykUserBindingDO obj = (ZykUserBindingDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}


	/**
	 * 下载导入模板
	 *
	 * @param response
	 */
	@GetMapping("/downloadImportTemplate")
	@ResponseBody
	public void downloadImportTemplate(HttpServletResponse response) {
		List<ZykUserBindingVO> list = new LinkedList<>();
		//导出操作
		EasyPoiUtil.exportExcel(list, "资源库用户权限信息导入模板", "资源库用户权限信息导入模板", ZykUserBindingVO.class, "资源库用户权限信息导入模板", response);
	}


	/**
	 * 培训证书信息导入
	 *
	 */
	@Log("培训证书信息导入")
	@PostMapping("/import")
	@ResponseBody
	@Transactional
	public Result<?> importExcel(@RequestParam("file") MultipartFile file,Long keyId){
		//第一步--判断文件格式
		if (!"application/vnd.ms-excel".equals(file.getContentType())) {
			return Result.fail("文件格式错误，请选择.xls文件");
		}
		//第二步--解析文件获得数据
		List<ZykUserBindingVO> list = EasyPoiUtil.importExcel(file, 1, 1, ZykUserBindingVO.class);

		List<ZykUserBindingDO> rstList =new ArrayList<>();
		for (int i=0;i<list.size();i++){
			ZykUserBindingDO zykUserBindingDO=new ZykUserBindingDO();
			zykUserBindingDO.setMobile(list.get(i).getMobile()); //手机号
			zykUserBindingDO.setUserName(list.get(i).getUserName()); //姓名
			zykUserBindingDO.setDepartment(list.get(i).getDepartment()); //部门
			zykUserBindingDO.setPosition(list.get(i).getPosition()); //职位
			zykUserBindingDO.setOrgName(list.get(i).getOrgName()); //单位名称
			zykUserBindingDO.setKeyId(keyId); //资源id
			zykUserBindingDO.setCreaterDate(new Date()); //创建时间
			rstList.add(zykUserBindingDO);
		}
		zykUserBindingService.insertBatch(rstList);
		return Result.ok("成功导入" + list.size() + "条数据");
	}





	@GetMapping("/add")
	String add(){
	    return "jzweb/zykUserBinding/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		ZykUserBindingDO zykUserBinding = zykUserBindingService.selectById(id);
		model.addAttribute("zykUserBinding", zykUserBinding);
	    return "jzweb/zykUserBinding/edit";
	}
	
	@Log("添加用户绑定信息导入")
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( ZykUserBindingDO zykUserBinding){
		zykUserBinding.setCreaterDate(new Date());
		zykUserBindingService.insert(zykUserBinding);
        return Result.ok();
	}
	
	@Log("修改用户绑定信息导入")
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update( ZykUserBindingDO zykUserBinding){
		boolean update = zykUserBindingService.updateById(zykUserBinding);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除用户绑定信息导入")
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Long id){
		zykUserBindingService.deleteById(id);
        return Result.ok();
	}

}
